package LawsonTransformJava.pgm;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.lang.reflect.InvocationTargetException;

import LawsonTransformJava.xref.CrossRef;
import LawsonTransformJava.xref.XrefEmpStatus;
import LawsonTransformJava.xref.XrefPosition;
import LawsonTransformJava.dao.DAOEmployee;
import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblEmployee;

/**
 * @author edavis
 *
 */
public class HR511 extends DAObject implements 
DAOEmployee, Runnable {
	
	private XrefPosition xPositions = null;
	private XrefEmpStatus xEmpStatuses = null;
//	private String[] xTermStsCodes = {"T1","T2","T3","TR"};
	private TblEmployee emp = new TblEmployee();

	private DecimalFormat df1 = new DecimalFormat("0.0000");
	
	/**
	 * @param output 		Output file to write records
	 * @param allowBlanks	Allow Blanks if a Cross Reference field is not found.
	 * 						Only allowed for non-required fields.
	 * @throws SQLException
	 */
	public HR511(File output, File errOutput, File rptOutput, boolean allowBlanks) throws SQLException {
		// initialization 
		try {
			this.output = new FileWriter(this.outputFile = output);
			this.errOutput = new FileWriter(this.errOutputFile = errOutput);
			this.errReport = new PRHistReport(rptOutput, new Date());
			init(allowBlanks);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
	}

	/**
	 * Initialization method that creates the data connection, cross reference DB connection and creates the
	 * appropriate Cross Reference mappings that are used in this Conversion Transformation Program.
	 * 
	 * @param allowBlanks - Allow Blanks if a Cross Reference field is not found.
	 * 						Only allowed for non-required fields.
	 * @throws IOException
	 * @throws SQLException
	 */
	private void init(boolean allowBlanks) throws IOException, SQLException {
		// Set the data and xref variables
		data = DAOFactoryOracleDB.getDAOOracleDB(true);
		xPositions = new XrefPosition();
		xEmpStatuses = new XrefEmpStatus();

		errReport.setPgmName(this.getClass().getSimpleName());

		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
		stmtData = data.getDataStatement();	    
		countStmtData = data.getCountStatement();
		//Set the Date Format for output
		df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	}

	public TblEmployee findEmployee() {
//		TblEmployee emp = new TblEmployee();
		try {
			if (whereClause==null || whereClause.equals("")) {
				whereClause = "WHERE " + TABLE_NAME2 + ".FIELD_KEY=99" +
							  " AND " + TABLE_NAME2 + ".EMP_APP=0";
			} else {
				whereClause = whereClause + " AND " + TABLE_NAME2 + ".FIELD_KEY=99" +
						  "  AND " + TABLE_NAME2 + ".EMP_APP=0";
			}
			sqlData = "SELECT " + TABLE_NAME + ".* " +
			          ", HREMPUSF.A_FIELD AS \"Employment_type\"" +
			          " FROM " + data.getSchema() + "." + TABLE_NAME +
			          " LEFT JOIN " + data.getSchema() + "." + TABLE_NAME2 +
			          " ON EMPLOYEE.COMPANY=HREMPUSF.COMPANY" +
			          "    AND EMPLOYEE.EMPLOYEE=HREMPUSF.EMPLOYEE" +
			          " " + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next()){
				hasMoreRecs = true;
				emp = (TblEmployee) createObject(rslt, new TblEmployee());
				emp.setPAEmployee((new HR511PAEmployee()).findEmployee(Integer.toString(emp.getEmployee())));
				if (emp.isTerminated())
					emp.setPAEmpPos((new HR511PAEmpPos()).findPAEmpPos(emp.getCompany(), Integer.toString(emp.getEmployee()), emp.getPosition(), new Date(System.currentTimeMillis())));
				else
					emp.setPAEmpPos((new HR511PAEmpPos()).findPAEmpPos(emp.getCompany(), Integer.toString(emp.getEmployee()), emp.getPosition(), emp.getTermination_date()));
			}
			else
				hasMoreRecs = false;
			return emp;
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return null;
	}

	public TblEmployee findEmployee(String employeeNumber) {
		try {
//			TblEmployee emp = new TblEmployee();
//			TblPAEmployee pae = null;
			whereClause = "WHERE " + TABLE_NAME + ".EMPLOYEE=" + employeeNumber + " AND HREMPUSF.FIELD_KEY=99" +
			  			  "  AND HREMPUSF.EMP_APP=0";
			sqlData = "SELECT " + TABLE_NAME + ".* " +
			          ", HREMPUSF.A_FIELD AS \"Employment_type\"" +
			          " FROM " + data.getSchema() + "." + TABLE_NAME +
			          " LEFT JOIN " + data.getSchema() + ".HREMPUSF" +
			          " ON EMPLOYEE.COMPANY=HREMPUSF.COMPANY" +
			          "    AND EMPLOYEE.EMPLOYEE=HREMPUSF.EMPLOYEE" +
			          " " + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
			emp = (TblEmployee) createObject(rslt, new TblEmployee());
			emp.setPAEmployee((new HR511PAEmployee()).findEmployee(Integer.toString(emp.getEmployee())));
			if (emp.isTerminated())
				emp.setPAEmpPos((new HR511PAEmpPos()).findPAEmpPos(emp.getCompany(), Integer.toString(emp.getEmployee()), emp.getPosition(), new Date(System.currentTimeMillis())));
			else
				emp.setPAEmpPos((new HR511PAEmpPos()).findPAEmpPos(emp.getCompany(), Integer.toString(emp.getEmployee()), emp.getPosition(), emp.getTermination_date()));
			return emp;
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return new TblEmployee();
	}

	public TblEmployee nextEmployee() {
		try {
			if (rslt.next()){
				hasMoreRecs = true;
				emp = (TblEmployee) createObject(rslt, new TblEmployee());
				emp.setPAEmployee((new HR511PAEmployee()).findEmployee(Integer.toString(emp.getEmployee())));
				if (emp.isTerminated())
					emp.setPAEmpPos((new HR511PAEmpPos()).findPAEmpPos(emp.getCompany(), Integer.toString(emp.getEmployee()), emp.getPosition(), new Date(System.currentTimeMillis())));
				else
					emp.setPAEmpPos((new HR511PAEmpPos()).findPAEmpPos(emp.getCompany(), Integer.toString(emp.getEmployee()), emp.getPosition(), emp.getTermination_date()));
			}
			else
				hasMoreRecs = false;
			return emp;
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return null;
	}

	public boolean hasMore () {
		return hasMoreRecs;
	}

	public int countEmployees() {
		try {
			whereClause = "WHERE HREMPUSF.FIELD_KEY=99" +
		  			  "  AND HREMPUSF.EMP_APP=0";
			sqlData = "SELECT COUNT(*) " +
			          " FROM " + data.getSchema() + "." + TABLE_NAME +
			          " LEFT JOIN " + data.getSchema() + "." + TABLE_NAME2 +
			          " ON EMPLOYEE.COMPANY=HREMPUSF.COMPANY" +
			          "    AND EMPLOYEE.EMPLOYEE=HREMPUSF.EMPLOYEE" +
			          " " + (whereClause==null?"":whereClause);
			countRecs = data.getCountStatement().executeQuery(sqlData);
			countRecs.next();
			return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
	}

	public void run() {
		try {
			//Update current date/time
			System.out.println(errReport.getName() + " Start " + Report.getDateFormat().format(new Date()));

			//@ TODO DEBUG BEGIN - remove lines
//					this.setWhereClause("WHERE EMPLOYEE=101107");
			//@ TODO DEBUG END   - remove lines
			emp = findEmployee();
			while (hasMore()) {
				//Increment Record Read
				errReport.addReadCnt();
				writeHR511Layout(emp);
				emp = nextEmployee();
			}
			//Write Record Counts
			errReport.writeRecordCounts(this.countEmployees());
			
			//Update current date/time
			System.out.println(errReport.getName() + " Finish " + Report.getDateFormat().format(new Date()));
			
			//Reclaim Resources
			fileCleanup();
			
		} catch (IOException e) {
			// Could not write properties file
			// Could not create output file
			System.out.println("IOException: " + e);
		}
	}

	//Output the HR511 format
	public void writeHR511Layout(TblEmployee emp) throws IOException {

		String writeValue = null;
//		if (Arrays.asList(xTermStsCodes).contains(emp.getEmp_status()))
//			write_pos_end_date = true;
		
		//Get Default values for Position
		XrefPosition.DftPos dftPosValues = xPositions.findDftPosValues(emp.getPosition());

		//Set value to write to appropriate outfile
		writeValue =
				"A" + "," +
				"1" + "," +
				emp.getEmployee() + "," +
				emp.getLast_name() + "," +
				emp.getFirst_name() + "," +
				emp.getMiddle_init() + "," +
				emp.getMiddle_name() + "," +
				emp.getNick_name() + "," +
				emp.getAddr1() + "," +
				emp.getAddr2() + "," +
				emp.getAddr3() + "," +
				emp.getAddr4() + "," +
				emp.getCity() + "," +
				emp.getState() + "," +
				emp.getZip() + "," +
				// TODO Default - blanks=US
				(emp.getCountry_code().equals("\"\"")?"\"US\"":emp.getCountry_code())  + "," +				//Default to US if = blanks
				//Employee Status translation is a combination of EMPLOYEE.Emp_Status and HREMPUSF.Employment_type
				xEmpStatuses.xlate(emp.getEmp_status()+emp.getEmployment_type()) + "," +
				emp.getFica_nbr() + "," +
				(dftPosValues!=null?dftPosValues.getProcessLevel():"") + "," +		//Process_level - Defaults from xRef
//				xProcessLevels.xlate(emp.getProcess_level()) + "," +				//Process_level
//				"," +																//Process_level - Defaults from POS
				(dftPosValues!=null?dftPosValues.getDepartment():"") + "," +		//Department - Defaults from xRef
//				xDepartments.xlate(emp.getDepartment()) + "," +						//Department
//				"," +																//Department - Defaults from POS
				"1," +																//Hm_dist_co - Defaults to 1
				(dftPosValues!=null?dftPosValues.getExpAcctUnit():"") + "," +		//Hm_acct_unit - Defaults from xRef
//				"," +																//Hm_acct_unit - Defaults from POS
				(dftPosValues!=null?dftPosValues.getExpAccount():"") + "," +		//Hm_account - Defaults from xRef
//				"," +																//Hm_account - Defaults from POS
				"," +																//Hm_sub_acct - Defaults from POS
				"," +
				emp.getActivity() + "," +
				(dftPosValues!=null?dftPosValues.getUnionCode():"") + "," +	//Union_code - Defaults from POS
//				xUnionCodes.xlate(emp.getUnion_code()) + "," +				//Union_code
//				"," +														//Union_code - Defaults from POS
				emp.getUser_level() + "," +
				emp.getDate_hired() + "," +
				emp.getAdj_hire_date() + "," +
				emp.getTerm_date() + "," +
				(dftPosValues!=null?dftPosValues.getJobCode():"") + "," +	//Job_code - Defaults from xRef
//				xJobCodes.xlate(emp.getJob_code()) + "," +					//Job_code
//				"," +														//Job_code - Defaults from POS
				df1.format(emp.getNbr_fte()) + "," +						//FTE - format for decimals
				(dftPosValues!=null?dftPosValues.getSalaryClass():"") + "," +	//Salary_class - Defaults from xRef
//				emp.getSalary_class() + "," +									//Salary_class
//				"," +															//Salary_class - Defaults from POS
				emp.getPay_rate() + "," +
				(dftPosValues!=null?dftPosValues.getPayFreq():"") + "," +	//Pay_frequency - Defaults from xRef
//				emp.getPay_frequency() + "," +								//Pay_frequency
//				"," +														//Pay_frequency - Defaults from POS
				emp.getStand_hours() + "," +
				emp.getStand_amt() + "," +
				// TODO Default Warning flag - Y
//				emp.getWarn_flag() + "," +									//Warning Flag
				"Y" + "," +													//Warning Flag - default Y
				emp.getAdd_allow_per() + "," +
				emp.getAdd_allow_hrs() + "," +
				emp.getAdd_allow_amt() + "," +
				// TODO Default Auto Time Rec blank=Y
				(emp.getAuto_time_rec().equals("\"\"")?"\"Y\"":emp.getAuto_time_rec()) + "," +	//Auto time record - Default to N if not found
//				"\"Y\"," +																		//Auto time record
				// TODO Default Auto Deposit to Y
//				emp.getAuto_deposit() + "," +		//Auto Deposit
				"\"Y\"," +							//Auto Deposit - Default to Y
				emp.getCreation_date() + "," +
				emp.getPay_step() + "," +
				emp.getPay_grade() + "," +
				emp.getSchedule() + "," +
				(dftPosValues!=null?dftPosValues.getExemptEmp():"") + "," +			//Exempt_emp - Defaults from xRef
//				emp.getExempt_emp() + "," +											//Exempt_emp
//				"," +																//Exempt_emp - Defaults from POS
				(dftPosValues!=null?dftPosValues.getShift():"") + "," +	//R_shift - Defaults from xRef
//				emp.getR_shift() + "," +								//R_shift
//				"," +													//R_shift - Defaults from POS
				emp.getEic_status() + "," +
				emp.getAnnivers_date() + "," +
				emp.getPension_plan() + "," +
				emp.getRpt_ins_cost() + "," +
				emp.getEbe_amount() + "," +
				(dftPosValues!=null?dftPosValues.getOtPlanCode():"") + "," +	//Ot_plan_code - Defaults from xRef
//				emp.getOt_plan_code() + "," +									//Ot_plan_code
//				"," +															//Ot_plan_code - Defaults from POS
				emp.getDeceased() + "," +
				emp.getBsi_group() + "," +
				emp.getWc_state() + "," +
				emp.getMoving_exp() + "," +
				emp.getSick_pay() + "," +
				emp.getPAEmployee().getSupp_addr1() + "," +
				emp.getPAEmployee().getSupp_addr2() + "," +
				emp.getPAEmployee().getSupp_addr3() + "," +
				emp.getPAEmployee().getSupp_addr4() + "," +
				emp.getPAEmployee().getSupp_city() + "," +
				emp.getPAEmployee().getSupp_state() + "," +
				emp.getPAEmployee().getSupp_zip() + "," +
				emp.getPAEmployee().getSupp_cntry_cd() + "," +
				emp.getPAEmployee().getHm_phone_cntry() + "," +
				emp.getPAEmployee().getHm_phone_nbr() + "," +
				emp.getPAEmployee().getWk_phone_cntry() + "," +
				emp.getPAEmployee().getWk_phone_nbr() + "," +
				emp.getPAEmployee().getWk_phone_ext() + "," +
				emp.getPAEmployee().getEeo_class() + "," +
				emp.getPAEmployee().getSex() + "," +
				emp.getPAEmployee().getHandicap_id() + "," +
				emp.getPAEmployee().getComp_code() + "," +
				emp.getPAEmployee().getComp_nbr() + "," +
				emp.getPAEmployee().getVeteran() + "," +
				emp.getPAEmployee().getBirthdate()				  + "," +
				emp.getPAEmployee().getBirth_city()				  + "," +
				emp.getPAEmployee().getBirth_state()			  + "," +
				emp.getPAEmployee().getBirth_cntry_cd()	  + "," +
				emp.getPAEmployee().getSenior_date()      	  + "," +
				emp.getPAEmployee().getMaiden_lst_nm()      	  + "," +
				emp.getPAEmployee().getMaiden_fst_nm()      	  + "," +
				emp.getPAEmployee().getMaiden_mi()      	  + "," +
				emp.getPAEmployee().getFormer_lst_nm()      	  + "," +
				emp.getPAEmployee().getFormer_fst_nm()      	  + "," +
				emp.getPAEmployee().getFormer_mi()      	  + "," +
				(dftPosValues!=null?dftPosValues.getLocateCode():"") + "," +				//Locat_code - Defaults from xRef
//				xLocatCodes.xlate(emp.getPAEmployee().getLocat_code())     	+ "," +			//Locat_code
//				"," +																		//Locat_code - Defaults from POS
				emp.getPAEmployee().getHire_source()      	  + "," +
				emp.getPAEmployee().getSecurity_code()      	  + "," +
				emp.getPAEmployee().getSecurity_nbr()      	  + "," +
				(dftPosValues!=null?dftPosValues.getBargainUnit():"") + 	"," +			//Bargain_unit - Defaults from xRef
//				emp.getPAEmployee().getBargain_unit()      	  + "," +						//Bargain_unit
//				"," +																		//Bargain_unit - Defaults from POS
				emp.getPAEmployee().getClock_nbr()      	  + "," +
				emp.getPAEmployee().getMail_group()      	  + "," +
				emp.getPAEmployee().getMb_nbr()      	  + "," +
				emp.getPAEmployee().getDraft_status()      	  + "," +
				emp.getPAEmployee().getFinal_rank()      	  + "," +
				emp.getPAEmployee().getCur_status()      	  + "," +
				emp.getPAEmployee().getRev_schedule()      	  + "," +
				emp.getPAEmployee().getNext_rev_code()      	  + "," +
				emp.getPAEmployee().getFnctn_group()      	  + "," +
				emp.getPAEmployee().getExclude_flag()      	  + "," +
				emp.getPAEmployee().getNbr_hl_dep()      	  + "," +
				emp.getPAEmployee().getNbr_dn_dep()      	  + "," +
				emp.getPAEmployee().getHl_cov_proof()      	  + "," +
				emp.getPAEmployee().getDn_cov_proof()      	  + "," +
				emp.getPAEmployee().getHl_verify_dt()      	  + "," +
				emp.getPAEmployee().getDn_verify_dt()      	  + "," +
				emp.getPAEmployee().getSpouse_emp()      	  + "," +
				emp.getPAEmployee().getSp_emp_addr1()      	  + "," +
				emp.getPAEmployee().getSp_emp_addr2()      	  + "," +
				emp.getPAEmployee().getSp_emp_addr3()      	  + "," +
				emp.getPAEmployee().getSp_emp_addr4()      	  + "," +
				emp.getPAEmployee().getSp_emp_city()      	  + "," +
				emp.getPAEmployee().getSp_emp_state()      	  + "," +
				emp.getPAEmployee().getSp_emp_zip()      	  + "," +
				emp.getPAEmployee().getSp_emp_country()      	  + "," +
				emp.getPAEmployee().getSp_emp_ph_cntr()      	  + "," +
				emp.getPAEmployee().getSp_emp_ph_nbr()      	  + "," +
				emp.getPAEmployee().getOwner_flag()      	  + "," +
				emp.getPAEmployee().getKey_emp_flag()      	  + "," +
				emp.getPAEmployee().getOfficer()      	  + "," +
				emp.getPAEmployee().getHigh_comp()      	  + "," +
				emp.getPAEmployee().getSmoker()      	  + "," +
				emp.getPAEmployee().getI9_status()      	  + "," +
				emp.getPAEmployee().getI9_alien_nbr()      	  + "," +
				emp.getPAEmployee().getI9_admit_nbr()      	  + "," +
				emp.getPAEmployee().getI9_sta_exp_dt()      	  + "," +
				emp.getPAEmployee().getI9_doc_nbr_01()      	  + "," +
				emp.getPAEmployee().getI9_doc_descr_01()      	  + "," +
				emp.getPAEmployee().getI9_doc_type_01()      	  + "," +
				emp.getPAEmployee().getI9_doc_exp_dt_01()      	  + "," +
				emp.getPAEmployee().getI9_doc_nbr_02()      	  + "," +
				emp.getPAEmployee().getI9_doc_descr_02()      	  + "," +
				emp.getPAEmployee().getI9_doc_type_02()      	  + "," +
				emp.getPAEmployee().getI9_doc_exp_dt_02()      	  + "," +
				emp.getPAEmployee().getI9_authorize() 	     	  + "," +
				emp.getPAEmployee().getTrue_mar_stat()      	  + "," +
				emp.getPAEmployee().getPrimary_care()   	   	  + "," +
				emp.getPAEmployee().getFamily_agg()      		  + "," +
				(dftPosValues!=null?dftPosValues.getPos():"") + 	"," +				//Position - Translate from xRef
//				xPositions.xlate(emp.getPosition()) + "," +
				"," +
				"," +
				"," +
				"," +
				emp.getTipped()				  + "," +
				emp.getPAEmployee().getWork_zip()	  + "," +
				emp.getAcct_category()			  + "," +
				emp.getPAEmployee().getSec_lvl()      	  + "," +
				emp.getPAEmployee().getSec_location()      	  + "," +		//Sec_location
				emp.getPAEmployee().getBen_salary_1()      	  + "," +
				emp.getPAEmployee().getBen_salary_2()      	  + "," +
				emp.getPAEmployee().getBen_salary_3()      	  + "," +
				emp.getPAEmployee().getBen_salary_4()      	  + "," +
				emp.getPAEmployee().getBen_salary_5()      	  + "," +
				emp.getPAEmployee().getBen_date_1()      	  + "," +
				emp.getPAEmployee().getBen_date_2()      	  + "," +
				emp.getPAEmployee().getBen_date_3()      	  + "," +
				emp.getPAEmployee().getBen_date_4()      	  + "," +
				emp.getPAEmployee().getBen_date_5()      	  + "," +
				emp.getPAEmpPos().getPos_level()			  + "," +
//				emp.getPAEmpPos().getEffect_date()			  + "," +			//Effect date
				(emp.getPAEmpPos().compareEffectTo(emp.getTermination_date())<0?emp.getPAEmpPos().getEffect_date():emp.getTerm_date())			  + "," +	//Effect date
				//Skip End date if terminated employee
//				(Arrays.asList(xTermStsCodes).contains(emp.getEmp_status().replaceAll("\"", ""))?emp.getPAEmpPos().getEnd_date() + ",":",")      +	//POS Term Date
				"," +															//POS Term Date
				emp.getSupervisor_ind()			  + "," +
				emp.getWork_sched()			  + "," +
				(dftPosValues!=null?dftPosValues.getAnnualHours():"") + "," +	//Annual_hours - Defaults from xRef
//				emp.getAnnual_hours()			  + "," +						//Annual_hours
//				"," +															//Annual_hours - Defaults from POS
				emp.getNew_hire_date()			  + "," +
				"," +
				// TODO Default - blanks=US
				(emp.getWork_country().equals("\"\"")?"\"US\"":emp.getWork_country())  + "," +				//Default to US if = blanks
//				emp.getPAEmployee().getLanguage_code()      	  + "," +		//Language Code
				"," +															//Language Code
				emp.getPAEmployee().getReligion()      	  + "," +
				emp.getPAEmployee().getDisability()   	  + "," +
				emp.getPAEmployee().getSupp_phone_cnt()      	  + "," +
				emp.getPAEmployee().getSupp_phone_nbr()      	  + "," +
				// TODO Default - MRS=MS
				(emp.getName_title().equalsIgnoreCase("\"MRS\"")?"\"MS\"":emp.getName_title()) + "," +		//Default to MS if = MRS
				emp.getName_suffix()			  + "," +
				emp.getCurrency_code()			  + "," +
				emp.getPAEmpPos().getBase_currency()	  + "," +
				emp.getPAEmpPos().getBase_pay_rate()	  + "," +
				emp.getLast_name_pre()			  + "," +
				emp.getPAEmployee().getConsent()      	  + "," +
				emp.getPAEmployee().getLast_review()      	  + "," +
				emp.getPAEmployee().getNext_review()      	  + "," +
				emp.getPAEmployee().getRelated_emp()      	  + "," +
				emp.getPAEmployee().getLife_style_cr()      	  + "," +
				emp.getEmail_address()			  + "," +
				emp.getMax_limit_ovrd()			  + "," +
				emp.getTax_province()			  + "," +
				emp.getBus_nbr_grp()			  + "," +
				emp.getEst_remun()			  + "," +
				emp.getEst_expense()			  + "," +
				emp.getQc_ent_nbr_grp()			  + "," +
				emp.getWc_province()			  + "," +
				emp.getPAEmployee().getAboriginal()	  + "," +
				emp.getPAEmployee().getVisible_min()	  + "," +
				emp.getRailroad_code()			  + "," +
				emp.getCounty()				  + "," +
				emp.getFst_day_worked()			  + "," +
				emp.getLast_day_paid()			  + "," +
				emp.getDeath_date()			  + "," +
				emp.getPAEmployee().getEstab_patient()	  + "," +
				emp.getPAEmployee().getPrior_cov_mo()	  + "," +
				emp.getPAEmployee().getSupp_county()	  + "," +
				emp.getPAEmployee().getStock_trade()	  + "," +
				emp.getLab_dist_flag()			  + "," +
				emp.getEncumber_flag()			  + "," +
				emp.getEffort_flag()			  + "," +
				emp.getRpt_curr()			  + "," +
				emp.getPrmcert_id()			  + "," +
				emp.getSndcert_id()			  + "," +
				emp.getFrng_rate()			  + "," +
				emp.getFrng_acct_cat()			  + "," +
				emp.getFrng_account()			  + "," +
				emp.getFrng_sub_acct()			  + "," +
				emp.getPAEmpPos().getDate_assign()	  + "," +
				emp.getPAEmployee().getUser_amount()	  + "," +
				emp.getTax_filter()			  + "," +
				emp.getRemote()				  + "," +
				emp.getTax_city()		  + "," +
				emp.getTax_county()			  + "," +
				emp.getTax_school()			  + "," +
				emp.getWork_city()			  + "," +
				emp.getWork_county()			  + "," +
				emp.getWork_school()			  + "," +
				emp.getPAEmployee().getMedicare_ind()	  + "," +
				emp.getPub_sec_retire()			  + "," +
				emp.getPrmcert_comp()			  + "," +
				emp.getSndcert_comp()			  + "," +
				"0"
			;
		
		if (CrossRef.isError()) {
			//Increment Record in Error
			errReport.addErrorCnt();
			//Write to Error File
			this.errOutput.write(writeValue + "\n");
			this.errOutput.flush();
			CrossRef.resetError();
		}
		else {
			//Increment Record Written
			errReport.addWrittenCnt();
			//Write to Load File
			this.output.write(writeValue + "\n");
			this.output.flush();
		}
	}
	
	public void fileCleanup() throws IOException {
		errReport.flush();
		errReport.close();
		output.flush();
		output.close();
		errOutput.flush();
		errOutput.close();
	}
}